/*
 * session.c
 *
 *   Copyright 2010 Emediate ApS
 *
 *   This file is part of Stampede.
 *
 *   Stampede is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   Stampede is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with Stampede.  If not, see <http://www.gnu.org/licenses/>.
 *
 *   Written by: Erland Lewin <erland@emediate.se>
 *
 *  Created on: Dec 22, 2009
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <libxml/xmlstring.h>

#include "session.h"
#include "transaction.h"

typedef struct sHeader
{
   xmlChar *name;
   xmlChar *value;

   xmlChar *string; // "name: value"
} sHeader;

typedef struct sSession
{
   xmlChar *name;
   int transactionCount;
   Transaction *transactions;

   int headerCount;
   Header *headers;
} sSession;

/*
 * Start of code
 */
Session session_create( const xmlChar *name, int headerCount, Header *headers,
                        int transactionCount, Transaction *transactions )
{
   Session session;

   session = malloc( sizeof( sSession ) );

   session->name = (xmlChar *) strdup( (char *) name);
   session->headerCount = headerCount;
   session->headers = headers;
   session->transactionCount = transactionCount;
   session->transactions = transactions;

   return session;
}

Transaction session_getTransactionByIndex( Session session, int index )
{
   if( index < session->transactionCount )
      return session->transactions[ index ];
   else
      return NULL;
}

const xmlChar *session_getHeaderStringByIndex( const Session session,
                                               int headerIndex )
{
   if( headerIndex < session->headerCount )
      return session->headers[ headerIndex ]->string;
   else
      return NULL;
}

int session_getHeaderCount( const Session session )
{
   return session->headerCount;
}
int session_getTransactionCount( const Session session )
{
   return session->transactionCount;
}
const xmlChar *session_getName( const Session session )
{
   return session->name;
}

Header header_create( const xmlChar *name, const xmlChar *value )
{
   Header header;
   xmlChar buf[ 1024 ];

   header = malloc( sizeof( sHeader ));

   header->name = (xmlChar *) strdup( (char *) name );
   header->value = (xmlChar *) strdup( (char *) value );

   sprintf( (char *) buf, "%s: %s", (char *) name, (char *) value );
   header->string = (xmlChar *) strdup( (char *) buf );

   return header;
}

const xmlChar *header_getString( const Header header )
{
   return header->string;
}

